<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import resize from './mixins/resize'

export default {
  name: 'KeyBar',
  mixins: [resize],
  props: {
    id: { type: String, default: 'chart' },
    className: { type: String, default: 'chart' },
    height: { type: String, default: '100%' },
    width: { type: String, default: '100%' },
    message: Object
  },
  data() {
    return {
      chart: null,
      options: this.message
        ? this.message
        : {
          grid: {
            left: '30',
            right: '30',
            bottom: '18%'
          },
          legend: {
            data: ['packect', 'bytes']
          },
          toolbox: {
            y: 'top',
            feature: {
              magicType: {
                type: ['line', 'bar', 'stack', 'tiled']
              },
              saveAsImage: {
                pixelRatio: 2
              }
            }
          },
          tooltip: {},
          xAxis: {
            data: [
              '2021-1',
              '2021-2',
              '2021-3',
              '2021-4',
              '2021-5',
              '2021-6',
              '2021-7',
              '2021-8',
              '2021-9',
              '2021-10',
              '2021-11',
              '2021-12'
            ]
          },
          yAxis: {},
          dataZoom: [
            {
              show: true,
              start: 20,
              end: 100
            },
            {
              type: 'inside',
              start: 20,
              end: 100
            }
          ],
          series: [
            {
              name: 'packect',
              type: 'bar',
              barWidth: '20%',
              data: [200, 20, 40, 180, 300, 50, 130, 100, 90, 220, 160, 30],
              emphasis: {
                focus: 'series'
              },
              itemStyle: {
                borderRadius: 4
              },
              animationDelay: function (idx) {
                return idx * 10 + 100
              }
            },
            {
              name: 'bytes',
              type: 'bar',
              barWidth: '20%',
              data: [20, 120, 140, 280, 30, 150, 230, 10, 190, 120, 60, 130],
              itemStyle: {
                opacity: 0.8,
                borderRadius: 4,
                shadowBlur: 2,
                shadowColor: '#111'
              },
              emphasis: {
                focus: 'series'
              }
            }
          ],
          animationEasing: 'elasticOut',
          animationEasingUpdate: 'elasticOut',
          animationDelay(idx) {
            return idx * 20
          },
          animationDelayUpdate(idx) {
            return idx * 20
          }
        }
    }
  },
  watch: {
    message: {
      handler(newValue) {
        this.options = newValue
        this.chart.setOption(newValue, true)
      }
    },
    id() {
      this.initChart()
    },
    deep: true
  },
  created() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let _this = this
      this.chart = this.$echarts.init(document.getElementById(this.id))
      // 切换图列后触发
      this.chart.on('legendselectchanged', function (params) {
        _this.$emit('clickSessionChart', params)
      })
      // 点击图表
      this.chart.on('click', function (params) {
        _this.$emit('clickChartItem', params)
      })
      this.chart.setOption(this.options, true)
    }
  }
}
</script>
